package com.googlecode.dgwt.client.dijit.view.render.base;

import com.google.gwt.dom.client.Element;
import com.googlecode.dgwt.client.dijit.view.store.CellView;
import com.googlecode.dgwt.client.dojo.data.model.ColumnSet;
import com.googlecode.dgwt.client.dojo.data.model.StoreColumn;
import com.googlecode.dgwt.client.dojox.data.base.EmptyStore;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public abstract class CellViewRenderBase extends CellRenderBase {

    public CellViewRenderBase(CellView view) {
        super(view);
    }

    @Override
    public CellView getView() {
        return (CellView) super.getView();
    }
    
    protected StoreColumn<Object,?> getColumn(int cellIndex){
        ColumnSet<? extends StoreColumn<Object,?>> columns = getView().getStore().getModel().getColumns();        
        if (cellIndex < columns.size()){
            return columns.get(cellIndex);
        }
        return EmptyStore.getEmptyColumn();
    }

    public void updateCell(int rowIndex, int cellIndex) {
        Object storeItem = getMetaInf(rowIndex);
        if (storeItem != null){
            internalUpdateCell(rowIndex, cellIndex, getColumn(cellIndex), storeItem);
        }
    }
    
    protected void internalUpdateCell(int rowIndex, int cellIndex, StoreColumn<Object, ?> column, Object storeItem){
        Element cellElement = getCell(rowIndex, cellIndex);
        column.onUpdateColumn(getView(), cellElement, storeItem);
        column.onFormatColumn(cellElement, storeItem);
    }

    
    @Override
    protected void internalUpdateRow(int rowIndex, Object metaInf) {
        if (metaInf != null){
            for (int cellIndex=0; cellIndex<getCellCount(); cellIndex++){
                internalUpdateCell(rowIndex, cellIndex, getColumn(cellIndex), metaInf);
            }
        }
    }    
    
    @Override
    public void updateCells(int rowIndex, int rowCount, int cellIndex, int cellCount) {
        for (int row=0; row<rowCount; row++){
            Object metaInf = getMetaInf(rowIndex);
            if (metaInf != null){
                for (int cell=0; cell<cellCount; cell++){
                    internalUpdateCell(rowIndex + row, cellIndex + cell, getColumn(cellIndex), metaInf);
                }
            }
        }
    }

    @Override
    public void updateCells(int cellIndex) {
        for (int rowIndex=0; rowIndex<getRowCount(); rowIndex++){
            Object metaInf = getMetaInf(rowIndex);
            if (metaInf != null){
                internalUpdateCell(rowIndex, cellIndex, getColumn(cellIndex), metaInf);
            }
        }
    }

    @Override
    public void updateCells(Object metaInf, int cellIndex) {
        if (metaInf != null){
            for (int rowIndex=0; rowIndex<getRowCount(); rowIndex++){
                if (metaInf.equals(getMetaInf(rowIndex))){
                    internalUpdateCell(rowIndex, cellIndex, getColumn(cellIndex), metaInf);
                }
            }
        }
    }

}
